<?php
class ChangePasswordForm extends sfForm
{
    public function setup()
    {
        $this->setWidgets(array(
            'password'         => new sfWidgetFormInputPassword(),
            'confirm_password' => new sfWidgetFormInputPassword()
        ));
        
        $this->setValidators(array(
            'password' => new sfValidatorString(array('min_length' => 6, 'max_length' => 50), array('min_length' => 'Too short(min 6 characters).', 'max_length'=> 'Too big.'))
        ));
        
        $this->validatorSchema->setOption('allow_extra_fields', true);
        $this->validatorSchema->setOption('filter_extra_fields', false);

        $this->validatorSchema->setPostValidator(new sfValidatorSchemaCompare('password','==', 'confirm_password'));
        
        $this->widgetSchema->setNameFormat('changepassword[%s]');
        
        $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

        parent::setup();
    }

    public function save($user_id)
    {
        $password = $this->getValue('password');

        $criteria = UserPeer::getActiveUserCriteria();
        $criteria->add('id', $user_id);
        
        $user = UserPeer::doSelectOne($criteria);

        if(!$user)
        {
            return false;
        }
        else{
            $user->setPassword($password);
            $user->save();

            return true;
        }
    }

    
}
?>